#include "stdafx.h"
#include <pthread.h>
#include "ServiceConfig.h"

SVR_PROXY_NAMESPACE_BEGIN

const char* g_szDefaultPropValue[] = {
	"", //beginProperty
	"p2p.server.service.info.url",
	"p2p.server.service.info.port",
	"p2p.server.service.info.hash",

	"p2p.server.host", 
	"p2p.server.port",
	"p2p.server.auth.host", 
	"p2p.server.auth.port",
	"p2p.server.backup.host",
	"p2p.server.backup.port",
	"p2p.server.meta.host",
	"p2p.server.meta.port",
	"p2p.server.web.host",
	"p2p.server.web.port",
	"p2p.server.content.host",
	"p2p.server.content.port",

	"p2p.server.path.service",
	"p2p.server.path.content",
	"p2p.server.path.thumbnail",
	"p2p.server.path.advertisement",

	"p2p.server.file.name.content",
	"p2p.server.file.name.category",
	"p2p.server.file.name.channel",
	"p2p.server.file.name.peer",
	"p2p.server.file.name.advertisement",

	"p2p.server.version.application.main",
	"p2p.server.version.application.sub",
	"p2p.server.version.protocol.main",
	"p2p.server.version.protocol.sub",

	"p2p.client.timeout", 
	"p2p.client.activeTime", 

	"p2p.server.msg.encoding", 
	"p2p.client.msg.encoding",
	"p2p.server.compress",
	"p2p.client.browser.runtime.path",
	"p2p.client.cache.base.dir",
	"p2p.client.cache.check.hash.enable",
	"p2p.client.peerlist.maxcounts",
	"", //end property
};

class ServiceConfigImpl : public ServiceConfig {
public:
	friend class SystemConfig;

	ServiceConfigImpl();
	virtual ~ServiceConfigImpl();
	virtual const char* GetProperty(const char* key);
	virtual const char* GetProperty(PropertyDef property);
	virtual void SetProperty(const char* key, const char* value);
	virtual void SetProperty(PropertyDef property, const char* value);

private:
	void LoadDefaults();

private:
	typedef tStringMap<string> tPropertyMap;
	tPropertyMap* m_propMap;
	void* m_rwlock;
};
static ServiceConfigImpl g_pSystemConfig;

ServiceConfigImpl::ServiceConfigImpl()
{
	m_rwlock = new pthread_rwlock_t;
	pthread_rwlock_init((pthread_rwlock_t*)m_rwlock, NULL);
	m_propMap = new tPropertyMap;
	LoadDefaults();
}

ServiceConfigImpl::~ServiceConfigImpl()
{
	pthread_rwlock_wrlock((pthread_rwlock_t*)m_rwlock);
	m_propMap->clear();
	SAFE_DELETE(m_propMap);
	pthread_rwlock_unlock((pthread_rwlock_t*)m_rwlock);
	pthread_rwlock_t* lock = (pthread_rwlock_t*)m_rwlock;
	pthread_rwlock_destroy((pthread_rwlock_t*)m_rwlock);
	SAFE_DELETE(lock);
	m_rwlock = NULL;
}

const char* ServiceConfigImpl::GetProperty(const char* key)
{
	pthread_rwlock_rdlock((pthread_rwlock_t*)m_rwlock);
	tPropertyMap::iterator iter = m_propMap->find(key);
	const char* sz = NULL;
	if(iter != m_propMap->end())
		sz = (*iter).second.c_str();
	pthread_rwlock_unlock((pthread_rwlock_t*)m_rwlock);
	return sz;
}

const char* ServiceConfigImpl::GetProperty(PropertyDef property)
{
	if(property <= beginProperty || property >= endProperty)
		return NULL;
	return GetProperty(g_szDefaultPropValue[property]);
}

void ServiceConfigImpl::SetProperty(const char* key, const char* value)
{
	if(value == NULL)
		return;
	pthread_rwlock_wrlock((pthread_rwlock_t*)m_rwlock);
	(*m_propMap)[key] = value;
	pthread_rwlock_unlock((pthread_rwlock_t*)m_rwlock);
}

void ServiceConfigImpl::SetProperty(PropertyDef property, const char* value)
{
	if(property <= beginProperty || property >= endProperty || value == NULL)
		return;
	SetProperty(g_szDefaultPropValue[property], value);
}

void ServiceConfigImpl::LoadDefaults()
{
	SetProperty(propAutoActiveTime, "50");
	char* url = getenv("DOCOURL");
	if(url != NULL)
		SetProperty(propServiceInfoURL, url);
	else
		SetProperty(propServiceInfoURL, "http://www.docomedia.com/DoCoService/ServiceInfo");
	SetProperty(propServiceInfoPort, "80");

	//SetProperty(propMainServerHost, "www.docomedia.com");
	//SetProperty(propMainServerPort, "80");
	//SetProperty(propMetaServerHost, "www.docomedia.com");
	//SetProperty(propMetaServerPort, "80");
#ifdef _DEBUG
	SetProperty(propConnectionTimeOut, "30");
#else
	SetProperty(propConnectionTimeOut, "10");
#endif
	SetProperty(propServerMsgEncoding, "UTF-8");
	SetProperty(propLocalMsgEncoding, "GBK");
	SetProperty(propServerEncryptData, "true");
	SetProperty(propMaxPeerListCounts, "16");

	SetProperty(propSecurityLogin, "true");


#ifndef P2P_DAEMON_SERVER
	char szPath[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, szPath);
	SetProperty(propBrowserRuntimeDir, szPath);
	string path = string(szPath);
	if(path.at(path.length() - 1 ) != '\\')
		path += '\\';
	path += "cache";
	SetProperty(propCacheBaseDir, path.c_str());
	SetProperty(propEnableCheckHash, "true");
#endif //P2P_DAEMON_SERVER
}

SVR_PROXY_NAMESPACE_END
